export const extTypeList = [
  { key: ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'tiff', 'tif'], value: 'image' },
  { key: ['mp4', 'mov', 'avi', 'mkv', 'flv', '3gp', 'webm', 'm3u8'], value: 'video' },
  { key: ['mp3', 'wav', 'ogg', 'aac', 'flac', 'wma'], value: 'audio' },
  { key: ['txt', 'html', 'htm', 'css', 'js', 'csv', 'md', 'xml'], value: 'text' },
  { key: ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'zip', 'rar', 'exe', 'json', 'apk', '7z', 'ai', 'cdr', 'class', 'dockerfile', 'gitignore', 'iso', 'jar', 'java', 'pr', 'psd', 'py', 'sql', 'yml'], value: 'application' }
]

export type AllowMimetypeType = 'image' | 'video' | 'audio' | 'text' | 'application'
export type MimetypeType = '' | AllowMimetypeType | AllowMimetypeType[]
// 判断资源的 mimetype 类型
export const getMimetype = (url: string): AllowMimetypeType => {
  if (url.lastIndexOf('.') === -1) return 'application'
  let ext = getFileExt(url)
  if (!ext) {
    ext = 'application'
  }
  let mimetype: AllowMimetypeType = 'application'
  extTypeList.forEach(item => {
    const hasExt = item.key.some(v => {
      return v === ext
    })
    if (hasExt) {
      mimetype = item.value as AllowMimetypeType
    }
  })
  return mimetype
}

// 获取资源拓展名
export const getFileExt = (url: string) => {
  if (url.lastIndexOf('.') === -1) return undefined
  if (url.substring(url.lastIndexOf('.') + 1).indexOf('?') > 0) {
    return url.substring(url.lastIndexOf('.') + 1).split('?')[0]
  } else {
    if (url.substring(url.lastIndexOf('.') + 1).indexOf('&') > 0) {
      return url.substring(url.lastIndexOf('.') + 1).split('&')[0]
    } else {
      return url.substring(url.lastIndexOf('.') + 1)
    }
  }
}
